package com.naiterui.ehp.bs.cms.modules.wms.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.naiterui.ehp.bp.bo.medication.MedicationDefaultUsageBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.wms.service.IMedicineDefaultUsageService;
import com.naiterui.ehp.bs.cms.modules.wms.vo.MedicineDefaultUsageDetailVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.MedicineDefaultUsageVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.MedicineUsageResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yangfuya
 * @since 2021/11/10
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = {SwaggerTag.CMS + SwaggerTag.MEDICINE_DRUG})
@RequestMapping("wms/medicine/sku/default/usage")
public class MedicineDefaultUsageController {
    private final IMedicineDefaultUsageService medicineDefaultUsageService;

    @SysLog("药品默认用法用量和安全用量列表")
    @ApiOperation(value = "药品默认用法用量和安全用量列表")
    @GetMapping("/list")
    @PreAuthorize("@p.check('wms:medicine:usage:list')")
    public PageVO<MedicineDefaultUsageVO> list(PageParamsVO pageParamsVO,
        MedicineDefaultUsageVO medicineDefaultUsageVO) {
        return this.medicineDefaultUsageService.queryPage(pageParamsVO, medicineDefaultUsageVO);
    }

    @ApiOperation(value = "药品默认用法用量和安全用量导入")
    @ApiImplicitParam(name = "file", value = "文件", dataType = "__file", paramType = "form", required = true)
    @PostMapping("/importExcel")
    @PreAuthorize("@p.check('wms:medicine:usage:import')")
    public MedicineUsageResultVO importExcel(@RequestParam("file") MultipartFile file) throws BusinessException {
        return this.medicineDefaultUsageService.importExpert(file);
    }

    @SysLog("药品配置模板地址")
    @ApiOperation(value = "药品配置模板地址")
    @GetMapping(value = "expert/model")
    @PreAuthorize("@p.check('user:doctor:expert:expert')")
    public ResponseVO<String> modelExpert() {
        return PublicService.returnResponseVO(this.medicineDefaultUsageService.modelExpert());
    }

    @SysLog("药品默认用法用量和安全用量详情")
    @ApiOperation(value = "药品默认用法用量和安全用量详情")
    @ApiImplicitParam(name = "id", value = "skuId", dataType = "long", paramType = "query", required = true)
    @GetMapping("/detail")
    @PreAuthorize("@p.check('wms:medicine:usage:detail')")
    public MedicineDefaultUsageDetailVO detail(@RequestParam Long id) throws BusinessException {
        return this.medicineDefaultUsageService.getById(id);
    }

    @SysLog("编辑药品默认用法用量和安全用量")
    @ApiOperation(value = "编辑默认用法用量和安全用量")
    @PostMapping("/edit")
    @PreAuthorize("@p.check('wms:medicine:usage:edit')")
    public void edit(@RequestBody MedicationDefaultUsageBO medicationDefaultUsageBO) throws BusinessException {
        medicineDefaultUsageService.edit(medicationDefaultUsageBO);
    }

}
